<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Floating Ball</title>
  <style>
    body {
      background-color: transparent;
      margin: 0;
      padding: 0;
      overflow: hidden; /* 禁止页面滚动 */
    }
    img {
      width: 100%;
      height: 100%;
    }
  </style>
</head>
<body>
  <img src="floating-ball.png" alt="Floating Ball">
  <script>
    const { ipcRenderer } = require('electron');
    window.ipcRenderer = ipcRenderer;

    let isDragging = false;
    let offsetX, offsetY;
    let lastSendTime = 0;
    const throttleTime = 16; // 节流时间，单位：毫秒
    let isMainWindowOpen = false; // 新增状态变量

    const ball = document.querySelector('img');

    // 阻止图片默认的拖动行为
    ball.ondragstart = function() {
        return false;
    };

    ball.addEventListener('mousedown', (e) => {
        isDragging = true;
        offsetX = e.clientX;
        offsetY = e.clientY;
        lastPosition.x = e.clientX;
        lastPosition.y = e.clientY;
    });

    document.addEventListener('mousemove', (e) => {
        const now = Date.now();
        if (isDragging && now - lastSendTime > throttleTime) {
            const dx = e.clientX - offsetX;
            const dy = e.clientY - offsetY;
            ipcRenderer.send('move-floating-ball', dx, dy);
            // offsetX = e.clientX;
            // offsetY = e.clientY;
            lastSendTime = now;
        }
    });

    document.addEventListener('mouseup', () => {
        isDragging = false;
    });

    // 点击事件
    /* ball.addEventListener('click', () => {
      if (isMainWindowOpen) {
        ipcRenderer.send('close-main-window'); // 发送关闭窗口的消息
      } else {
        ipcRenderer.send('open-main-window'); // 发送打开窗口的消息
      }
      isMainWindowOpen = !isMainWindowOpen; // 切换状态
    });*/
    
    // 双击事件
    ball.addEventListener('dblclick', () => {
      if (isMainWindowOpen) {
        ipcRenderer.send('close-main-window'); // 发送关闭窗口的消息
      } else {
        ipcRenderer.send('open-main-window'); // 发送打开窗口的消息
      }
      isMainWindowOpen = !isMainWindowOpen; // 切换状态
    });
    
    // 监听右键点击事件
    document.addEventListener('contextmenu', (e) => {
      e.preventDefault(); // 阻止默认的右键菜单
      // 发送包含点击位置的消息到主进程
      ipcRenderer.send('show-context-menu', { x: e.clientX, y: e.clientY });
    });
  </script>
</body>
</html>